package com.example.memorypoint.activity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.memorypoint.R;
import com.example.memorypoint.adapter.FragmentAdapter;
import com.example.memorypoint.entity.Diary;
import com.example.memorypoint.fragment.DiaryFragment;

import java.util.ArrayList;
import java.util.List;

public class DisplayDiariesActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private FragmentAdapter adapter;
    private List<DiaryFragment> diaryFragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_diaries);

        viewPager = findViewById(R.id.viewPager);

        // 获取传递过来的日记数据
        ArrayList<Diary> selectedDiaries = getIntent().getParcelableArrayListExtra("selected_diaries");

        // 将每个日记转换为 Fragment 并添加到 list
        if (selectedDiaries != null) {
            for (Diary diary : selectedDiaries) {
                diaryFragments.add(DiaryFragment.getInstance(diary));
            }
        }

        // 设置 ViewPager 和 Adapter
        adapter = new FragmentAdapter(getSupportFragmentManager(), diaryFragments);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2); // 设置预加载页面数
    }
}
